package com.second.controller.classify;

import com.github.pagehelper.PageInfo;
import com.second.model.dto.LAndR.LAndRResult;
import com.second.model.dto.classify.SeckillGoodsDto;
import com.second.model.pojo.FShop;
import com.second.service.IFOrdSeckillService;
import com.second.service.impl.FShopServiceImpl;
import com.second.task.SendSecMsgTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("fr")
public class ClassifyController extends HttpServlet {
    @Autowired

    private FShopServiceImpl shopsService;
    @Autowired
    HttpServletRequest httpServletRequest;

    @Autowired
    private IFOrdSeckillService ordSeckillService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SendSecMsgTask sendSecMsgTask;

    @Autowired
    private HttpServletRequest request;

    @GetMapping("classify")
    public String foodIndex() {
        List<SeckillGoodsDto> list = ordSeckillService.findAllSeckillGoods();
        request.setAttribute("seckillList",list);
        return "jsp/classify/classify";
    }
//价格分类（待修改）
@PostMapping("price/{consumption1}/{consumption2}/{pageNum}")
@ResponseBody
public LAndRResult price(@PathVariable(name = "consumption1") Double consumption1,@PathVariable(name = "consumption2") Double consumption2,@PathVariable(name = "pageNum") Integer pageNum){
    if ( null == pageNum) {
        pageNum = 1;
    }
//        Double pirce=fShop.getConsumption();
    PageInfo<FShop> shopList=shopsService.price(consumption1,consumption2,pageNum,5);
    System.out.println(shopList);
    return new LAndRResult("200","价格",shopList);

}
//    类型分类
    @PostMapping("type1/{type}/{pageNum}")
    @ResponseBody
    public LAndRResult type(@PathVariable(name = "type") Integer type,@PathVariable(name = "pageNum") Integer pageNum){
        if ( null == pageNum) {
            pageNum = 1;
        }
        PageInfo<FShop> shopList=shopsService.findByType(type,pageNum,5);

        System.out.println(shopList);

        return new LAndRResult("200","类型",shopList);

    }
//    地区分类
    @PostMapping("area/{area}/{pageNum}")
    @ResponseBody
    public LAndRResult place2( @PathVariable(name = "area") String area,@PathVariable(name = "pageNum") Integer pageNum) {
        if ( null == pageNum) {
            pageNum = 1;
        }
        PageInfo<FShop> shopList1=shopsService.findAllByArea(area,pageNum,5);
        System.out.println(shopList1);
        return new LAndRResult("200","类型",shopList1);
    }
    @PostMapping("score/{pageNum}")
    @ResponseBody
    public LAndRResult score( @PathVariable(name = "pageNum") Integer pageNum) {
        FShop fShop=new FShop();
       PageInfo<FShop> shopList2=shopsService.findAllByShopScore(pageNum,10);

        return new LAndRResult("200","类型",shopList2);
    }
    @PostMapping("select/{shopName}/{pageNum}")
    @ResponseBody
    public LAndRResult select(@PathVariable(name = "shopName")  String shopName, @PathVariable(name = "pageNum") Integer pageNum) {
        System.out.println(shopName);
        FShop fShop=new FShop();
        PageInfo<FShop> shopList2=shopsService.search(shopName,pageNum,5);
        System.out.println(shopList2);
        return new LAndRResult("200","类型",shopList2);
    }

    //秒杀券
    @PostMapping("seckill/{userId}/{secId}")
    @ResponseBody
    public String seckillGoods(@PathVariable(name = "userId")String userId,@PathVariable(name = "secId") Integer secId) {
        System.out.println("userId:"+userId);
        //看是否有用户登录
        if (null != userId) {
            //校验秒杀活动是非开始 //校验秒杀活动是否能够进行
            if (ordSeckillService.isEnd(secId).equals("200")) {
                //定义一个redisList
                ListOperations lop = redisTemplate.opsForList();
                System.out.println("进入秒杀");
                //如果可以移除说明还有商品
                if (null != lop.leftPop("seckill" + secId)) {
                    System.out.println("秒杀中。。。");
                    //创建一个存放购买用户名的set集合
                    SetOperations sops = redisTemplate.opsForSet();
                    //校验是否之前购买过
                    if (sops.isMember("seckill_result" + secId, userId)) {
                        return "sorry，您已经购买过了！";
                    } else {
                        sops.add("seckill_result" + secId, userId);
                        //秒杀成功生成订单。。
                        Map<String, Object> msg = new HashMap<>();
                        msg.put("userId", userId);
                        msg.put("secId", secId);
                        sendSecMsgTask.sendSecMsg(msg);
                        return "秒杀成功,生成订单。。。请尽快支付";
                    }
                } else return "商品已经售罄!!";
            } else return "活动还没开始";
        }
        //正常可以秒杀
        else return "请先登录";
    }

    /**
     * 过期的下架
     * @param secId
     */
    @PostMapping("cancelSeckill/{secId}")
    @ResponseBody
    public void seckillGoods(@PathVariable(name = "secId") Integer secId) {
        ordSeckillService.updateStatus(secId);
    }

}
